package com.ruoyi.zhoubao.tool;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户列表接口测试类（包含认证）
 */
public class UserListApiTestWithAuth {

    private static final String BASE_URL = "http://47.108.14.157:8080";
    private static final String LOGIN_URL = BASE_URL + "/login";
    private static final String API_URL = BASE_URL + "/zhoubao/zhoubao_Base/list";

    /**
     * 执行登录获取会话
     */
    public static String loginAndGetSession() {
        try {
            URL url = new URL(LOGIN_URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法和属性
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setDoOutput(true);

            // 登录参数
            String postData = "username=query&password=123456";

            // 发送登录请求
            try (OutputStream os = connection.getOutputStream()) {
                os.write(postData.getBytes());
                os.flush();
            }

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Login Response Code: " + responseCode);

            // 获取会话cookie
            String cookies = connection.getHeaderField("Set-Cookie");
            System.out.println("Session Cookie: " + cookies);

            connection.disconnect();
            return cookies;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 测试获取用户列表接口
     */
    public static void testGetUserList() {
        try {
            // 先登录获取会话
            String sessionCookie = loginAndGetSession();

            if (sessionCookie == null) {
                System.out.println("登录失败，无法获取会话信息");
                return;
            }

            // 创建API URL对象
            URL url = new URL(API_URL);

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法
            connection.setRequestMethod("POST");

            // 设置请求头，包含认证信息
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("Cookie", sessionCookie);

            // 设置连接超时时间
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("API Response Code: " + responseCode);

            // 读取响应内容
            BufferedReader reader;
            if (responseCode == HttpURLConnection.HTTP_OK) {
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            } else {
                reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            }

            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = reader.readLine()) != null) {
                response.append(inputLine);
            }
            reader.close();

            // 输出响应内容
            System.out.println("API Response Body: " + response.toString());

            // 断开连接
            connection.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 主方法
     */
    public static void main(String[] args) {
        testGetUserList();
    }
}
